import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import '../../helper/routeHelper.dart';
import '../../model/daydata.dart';
import '../../model/scsq_q.dart';
import '../../model/scsq_sk.dart';
import '../../model/scsq_z.dart';
import '../../pages/sdzx/sdzx_helper.dart';
import '../../pages/sdzx/skdata.dart';
import '../../helper/Helper.dart';
import '../../helper/channelHelper.dart';
import '../../widgets/IconWidget.dart';
import '../../widgets/inputWidget.dart';
import 'package:latlong2/latlong.dart';

const ShowColor1 = Colors.blue;
const ShowColor2 = Colors.blue;

class SdzxDataPage extends StatefulWidget {
  @override
  SdzxDataPage();

  @override
  SdzxDataPageState createState() => new SdzxDataPageState();
}

class SdzxDataPageState extends State<SdzxDataPage> {
  late SKData1 sk;
  late SKData2 sk2;
  bool ifLoaded = false;
  late List<ScsqQ> qList;
  late ShuiZhi sz;
  late List<ShuiZhi> zList;
  late List<ScsqSK> skList1;
  late List<DayData> skList2;
  late DateTime tm1;
  late DateTime tm2;
  late DateTime tm3;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    LoadData();
  }

  void LoadData() async {
    tm1 = DateTime.now();
    tm1 = DateTime(tm1.year, tm1.month, tm1.day, tm1.hour, 0, 0);
    tm2 = tm1.add(new Duration(hours: -24));
    tm3 = tm1.add(new Duration(days: -30));

    sk = await SKData1.GetNew();
    print(sk.toJson());
    sk2 = await SKData2.GetNew();
    print(sk2.toJson());
    qList = await ScsqQ.GetNewList();
    print(qList);
    sz = (await ShuiZhi.GetNew())!;
    print(sz.toJson());
    zList = await ShuiZhi.GetListByTM(
        "岷江紫坪铺水库", Helper.DateTimeToString(tm3), Helper.DateTimeToString(tm1));
    skList1 = await ScsqSK.GetListByTM(
        Helper.DateTimeToString(tm2), Helper.DateTimeToString(tm1));
    skList2 = await DayData.GetListByTM(
        Helper.DateTimeToString(tm3), Helper.DateTimeToString(tm1));
    print(Helper.DateTimeToString(tm1));
    //print(sz);
    setState(() {
      ifLoaded = true;
    });
    ChannelHelper.showToast("更新完成!");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //backgroundColor: Colors.white,
      // appBar: new AppBar(
      // title: bottomTabs[currentIndex].title,
      //title: Text("断面详情")),
      floatingActionButton: getFloatingButton(context),
      body: SdzxDataBody(context),
    );
  }

  Widget getFloatingButton(BuildContext context) {
    return new FloatingActionButton(
      child: Icon(Icons.refresh),
      onPressed: () {
        LoadData();
      },
    );
  }

  Icon getZL(String st) {
    if (st == "涨")
      return Icon(
        FontAwesomeIcons.arrowUp,
        size: 12.0,
      );
    if (st == "落")
      return Icon(
        FontAwesomeIcons.arrowDown,
        size: 12.0,
      );

    // ignore: prefer_const_constructors
    return Icon(
      FontAwesomeIcons.arrowRight,
      size: 12.0,
    );
  }

  Widget getQTile(ScsqQ s1) {
    var v1 = Text(
      s1.STNM,
      style: TextStyle(fontSize: 14.0, color: Colors.black87),
    );
    var v2 = getZL(s1.P);
    var v3 = Text(
      Helper.RoundDouble(s1.Q, 0, " "),
      style: TextStyle(
          fontSize: 16.0, color: ShowColor1, fontWeight: FontWeight.bold),
    );

    return InputWidget.getShowInfo2(
        s1.STNM,
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [v3, v2],
        ),
        Colors.blue,
        onTap: () => showQTimeLine(s1));
  }

  void showQTimeLine(ScsqQ s1) async {
    var qList = await ScsqQ.GetListByTM(
        s1.STCD, Helper.DateTimeToString(tm2), Helper.DateTimeNowString());
    if (qList == null) return;

    RouteHelper.showBottomPanel(context, SdzxHelper.getQTimeLine(qList));
  }

  void showZTimeLine(String cs) async {
    RouteHelper.showBottomPanel(context, SdzxHelper.getZTimeLine(zList, cs));
  }

  void showSKTimeLine1(String cs) async {
    RouteHelper.showBottomPanel(
        context, SdzxHelper.getSKTimeLine1(skList1, cs));
  }

  void showSKTimeLine2(String cs) async {
    RouteHelper.showBottomPanel(
        context, SdzxHelper.getSKTimeLine2(skList2, cs));
  }

  List<Widget> getWidgetList(BuildContext context) {
    List<Widget> list1 = <Widget>[];
    List<Widget> list2 = <Widget>[];
    List<Widget> list3 = <Widget>[];
    List<Widget> list4 = <Widget>[];
    List<Widget> list5 = <Widget>[];
    List<Widget> list6 = <Widget>[];

    list1.add(InputWidget.getGreyInfo("水库实时数据", Helper.DateTimeFormat(sk.TM)));

    var v1 = InputWidget.getShowInfo(
        "入库流量", sk.R.toStringAsFixed(0), ShowColor1,
        onTap: () => showSKTimeLine1("入库流量"));
    var v2 = InputWidget.getShowInfo(
        "出库流量", sk.Q.toStringAsFixed(0), ShowColor1,
        onTap: () => showSKTimeLine1("出库流量"));
    var v3 = InputWidget.getShowInfo(
        "水库水位", sk.H.toStringAsFixed(2), ShowColor1,
        onTap: () => showSKTimeLine1("水库水位"));
    var v4 = InputWidget.getShowInfo(
        "日均入库", sk.RD.toStringAsFixed(0), ShowColor1,
        onTap: () => showSKTimeLine2("日均入库"));
    var v5 = InputWidget.getShowInfo(
        "日均出库", sk.QD.toStringAsFixed(0), ShowColor1,
        onTap: () => showSKTimeLine2("日均出库"));
    var v6 = InputWidget.getShowInfo(
        "可调水量", (sk.KT / 10000).toStringAsFixed(2), ShowColor1,
        onTap: () => showSKTimeLine2("日均水位"));
    list4.add(InputWidget.getShowRow([v3, v1, v2], true));
    list4.add(InputWidget.getDivider());
    list4.add(InputWidget.getShowRow([v6, v4, v5], true));
    list1.add(InputWidget.inCard3(list4));

    var f1 = InputWidget.getShowInfo(
        "昨日降雨", sk2.R1.toStringAsFixed(1), ShowColor2,
        onTap: () => showSKTimeLine2("日均降雨量"));
    var f2 = InputWidget.getShowInfo(
        "近3日降雨", sk2.R2.toStringAsFixed(1), ShowColor2,
        onTap: () => showSKTimeLine2("日均降雨量"));
    var f3 = InputWidget.getShowInfo(
        "近7日降雨", sk2.R3.toStringAsFixed(1), ShowColor2,
        onTap: () => showSKTimeLine2("日均降雨量"));
    var f4 = InputWidget.getShowInfo(
        "昨日发电量", sk2.E1.toStringAsFixed(0), ShowColor2,
        onTap: () => showSKTimeLine2("日均发电量"));
    var f5 = InputWidget.getShowInfo(
        "本月累计", sk2.E2.toStringAsFixed(0), ShowColor2,
        onTap: () => showSKTimeLine2("日均发电量"));
    var f6 = InputWidget.getShowInfo(
        "今年累计", sk2.E3.toStringAsFixed(0), ShowColor2,
        onTap: () => showSKTimeLine2("日均发电量"));

    list1.add(Container(height: 6.0));
    list1
        .add(InputWidget.getGreyInfo("发电统计数据", Helper.DateTimeFormat2(sk2.TM)));
    list5.add(InputWidget.getShowRow([f4, f5, f6], true));
    list1.add(InputWidget.inCard3(list5));

    list1.add(Container(height: 6.0));
    list1
        .add(InputWidget.getGreyInfo("流域降雨数据", Helper.DateTimeFormat2(sk2.TM)));
    list6.add(InputWidget.getShowRow([f1, f2, f3], true));
    list1.add(InputWidget.inCard3(list6));

    list1.add(Container(height: 6.0));
    list1.add(InputWidget.getGreyInfo(
        "河道实时流量", Helper.DateTimeFormat(qList.first.TM)));
    list3.add(
        InputWidget.getShowRow([getQTile(qList[0]), getQTile(qList[1])], true));
    list3.add(InputWidget.getDivider());
    list3.add(
        InputWidget.getShowRow([getQTile(qList[2]), getQTile(qList[4])], true));
    list3.add(InputWidget.getDivider());
    list3.add(
        InputWidget.getShowRow([getQTile(qList[3]), getQTile(qList[5])], true));
    list1.add(InputWidget.inCard3(list3));

    list1.add(Container(height: 6.0));
    // list1.add(InputWidget.getGreyInfo(
    //     "库区实时水质", Helper.DateTimeFormat(sz.PUBLISHTIME)));
    var x1 = InputWidget.getShowInfo("水质标准", sz.CWQI, ShowColor2,
        onTap: () => showZTimeLine("水质标准"));
    var x2 = InputWidget.getShowInfo("高锰酸盐", sz.COL3, ShowColor2,
        onTap: () => showZTimeLine("高锰酸盐"));
    var x3 = InputWidget.getShowInfo("氨氮", sz.COL2, ShowColor2,
        onTap: () => showZTimeLine("氨氮"));
    var x4 = InputWidget.getShowInfo("总磷", sz.COL1, ShowColor2,
        onTap: () => showZTimeLine("总磷"));
    var x5 = InputWidget.getShowInfo("浊度", sz.COL14, ShowColor2,
        onTap: () => showZTimeLine("浊度"));
    var x6 = InputWidget.getShowInfo("PH值", sz.COL11, ShowColor2,
        onTap: () => showZTimeLine("PH值"));
    var x7 = InputWidget.getShowInfo("水温", sz.COL12, ShowColor2);
    var x8 =
        InputWidget.getShowInfo("超标因子", getSZCB(sz.NOREACHFACTS), ShowColor2);
    var x9 = InputWidget.getShowInfo("总氮", sz.COL19, ShowColor2,
        onTap: () => showZTimeLine("总氮"));
    var x10 = InputWidget.getShowInfo("溶解氧", sz.COL6, ShowColor2,
        onTap: () => showZTimeLine("溶解氧"));

    list2.add(InputWidget.getShowRow([x1, x8, x10], true));
    list2.add(InputWidget.getDivider());
    list2.add(InputWidget.getShowRow([x3, x4, x9], true));
    list2.add(InputWidget.getDivider());
    list2.add(InputWidget.getShowRow([x5, x2, x6], true));
    // list1.add(InputWidget.inCard3(list2));

    list1.add(Container(
      height: 56.0,
      child: Text(""),
    ));

    return list1;
  }

  String getSZCB(String cb) {
    if (cb == "-") return "无";
    return cb;
  }

  SdzxDataBody(BuildContext context) => SingleChildScrollView(
        child: SdzxDataFields(context),
      );

  SdzxDataFields(BuildContext context) {
    if (!ifLoaded)
      return Center(
          child: SpinKitCircle(
        color: Colors.blue,
      ));
    return Container(child: Column(children: getWidgetList(context)));
  }
}
